<?php

namespace app\index\dao;

use app\common\model\AccountBill;
use app\common\model\LawyerImpression;
use app\common\model\Member;
use app\common\model\Trade;
use app\index\traits\MatchRecorderTrait;
use ShidianUtil\Thinkphp\Dao\DaoTrait;
use think\Collection;

/**
 * HotRankDao 数据访问对象
 * Class HotRankDao
 * @package app\dao
 * @method Member findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method Member insertActive(array $params, array $allow_field = [])
 * @method Member updateActive(Member $model, array $params, array $allow_field = [])
 * @method Member findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method Member updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(Member $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class HotRankDao
{
    use DaoTrait;
    use MatchRecorderTrait;

    /**
     * @var Member
     */
    private $baseModel;

    /**
     * MemberDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = Member::class;
    }

    /**
     * 案源消耗榜
     * @param $startTime
     * @param $endTime
     * @param $page
     * @param $page_size
     * @return Collection
     */
    public function selectHotRankSource($startTime, $endTime, $page, $page_size): Collection
    {
        $subSql = Trade::where('tradeType', TRADE_SOURCE)
            ->when(is_numeric($startTime), fn($query) => $query->where('payTime', '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where('payTime', '<=', $endTime))
            ->where('payStatus', Trade::PAID)
            ->field([
                'count(*)' => 'val',
                'memberId'
            ])
            ->group('memberId')
            ->buildSql();

        return $this->select($subSql, $page, $page_size);
    }

    /**
     * 接单收入榜
     * @param $startTime
     * @param $endTime
     * @param $page
     * @param $page_size
     * @return Collection
     */
    public function selectHotRankIncome($startTime, $endTime, $page, $page_size): Collection
    {

        $subSql = AccountBill::where('billType', TRANSFER_SHARE_LAWYER)
            ->when(is_numeric($startTime), fn($query) => $query->where('created', '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where('created', '<=', $endTime))
            ->where('toType', 'member')
            ->field([
                'sum( `money` )' => 'val',
                'toMemberId' => 'memberId'
            ])
            ->group('memberId')
            ->buildSql();

        return $this->select($subSql, $page, $page_size);
    }

    /**
     * 曝光榜
     * @param $startTime
     * @param $endTime
     * @param $page
     * @param $page_size
     * @return Collection
     */
    public function selectHotRankImpression($startTime, $endTime, $page, $page_size): Collection
    {
        $subSql = LawyerImpression::when(is_numeric($startTime), fn($query) => $query->where('created', '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where('created', '<=', $endTime))
            ->field([
                'count(*)' => 'val',
                'lawyerId' => 'memberId'
            ])
            ->group('lawyerId')
            ->buildSql();

        return $this->select($subSql, $page, $page_size);
    }

    /**
     * 排行榜查询
     * @param string $subSql 排序子查询
     * @param $page
     * @param $page_size
     * @return Member[]|array|Collection
     */
    private function select(string $subSql, $page, $page_size)
    {
        $tableName = Member::getTable();

        return Member::leftJoin($subSql . ' sub', "$tableName.id=sub.memberId")
            ->where("$tableName.type", '1')
            ->where("$tableName.subType", '0')
            ->where('status', '1')
            ->whereNotNull('memberId')
            ->tableField(['id', 'type', 'nickname', 'realname', 'headImgUrl', 'cropImgUrl'], $tableName)
            ->tableField(['val',], 'sub')
            ->append(['headImg', 'headImgThumb', 'cropImg', 'showName'])
            ->hidden(['headImgUrl', 'cropImgUrl'])
            ->order('sub.val', 'DESC')
            ->order('id', 'ASC')
            ->paging($page, $page_size)
            ->select();
    }
}
